Programming Paradigms (Procedural and Object-oriented)
87 questions· page 1 of 9
Write program code to declare and initialise Queue, HeadPointer, TailPointer and NumberItems
Save your program as Question1_J25.
Copy and paste the program code into part 1(a) in the evidence document.
The function Enqueue():
- takes an integer as a parameter
- checks if the queue is full
- returns
FALSEif the queue is full - stores the parameter in the next position in the queue and returns
TRUEif the queue is not full - updates the appropriate pointers and
NumberItems
Write program code for Enqueue()
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
The main program:
- attempts to store each of the integers 1 to 25 (inclusive) in the queue in ascending numerical order using
Enqueue() - outputs the integer that was passed to
Enqueue()and "Successful" if it was stored in the queue, or "Unsuccessful" if it was not stored in the queue.
For example:- if the integer 5 is passed to
Enqueue()and is stored in the queue, the output will be: "5 Successful" - if the integer 23 is passed to
Enqueue()and is not stored in the queue, the output will be: "23 Unsuccessful"
- if the integer 5 is passed to
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
The function Dequeue() returns -1 if the queue is empty. If the queue is not empty, the function returns the next item in the queue, updates the appropriate pointers and updates NumberItems
Write program code for Dequeue()
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
Write program code to extend the main program to call Dequeue() twice and output the return value each time.
Save your program.
Copy and paste the program code into part 1(e)(i) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 1(e)(ii) in the evidence document.
The function ReadData():
- prompts the user to enter a filename and reads this filename from the user
- opens the file and reads each line of data into a 1D array
- returns the populated 1D array.
The function needs to work for a file that contains an unknown number of lines.
Write program code for ReadData()
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
The procedure SplitData() takes a 1D string array as a parameter with the identifier DataArray
The procedure declares six 1D arrays: one array for each colour that appears in the file (red, green, blue, orange, yellow, pink).
The procedure accesses each string in DataArray. The data in each string is split into the integer and the colour. The integer is stored in the array that matches the colour.
For example, the first string in DataArray has the integer 10 and the colour red, so the integer 10 is stored in the array for the colour red.
Write program code for SplitData()
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
The procedure StoreData():
- takes two parameters: a 1D array
DataToStoreand a filename - opens the text file with the filename that is passed as a parameter
- appends each item of data from
DataToStoreto a new line in the text file - uses exception handling when opening and writing data to the text file.
Write program code for StoreData()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
Each of the six colours has a blank text file where the numbers will be stored. The names of these six text files are:
Blue.txtGreen.txtOrange.txtPink.txtRed.txtYellow.txt
The procedure SplitData() needs amending to call StoreData() six times, with each of the six colour arrays and the name of the text file that corresponds to that colour.
For example, StoreData() will be called with the red array and the file name "Red.txt"
Write program code to amend SplitData()
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(e)(i) in the evidence document.
Test your program. Input the text "TheData.txt" when prompted.
Take a screenshot of the output(s) and a screenshot showing the content of the file that stores the red numbers.
Save your program.
Copy and paste the screenshot(s) into part 2(e)(ii) in the evidence document.
The pseudocode record format is:
TYPE NewRecord
DECLARE Key : INTEGER
DECLARE Item1 : INTEGER
DECLARE Item2 : INTEGER
ENDTYPE
Write program code to declare the record type NewRecord
If your chosen programming language does not support record formats, a class can be used instead.
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
Write program code to declare the global arrays HashTable and Spare
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
An empty record has the integer -1 stored in each field.
The procedure Initialise() stores an empty record in each element in HashTable and Spare
Write program code for Initialise()
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
The hash value is calculated from the key field using the formula: Key MOD 200
The function CalculateHash() takes a key field as a parameter and calculates and returns the hash value for the key field.
Write program code for CalculateHash()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
The procedure InsertIntoHash():
- takes a record of type
NewRecordas a parameter - uses
CalculateHash()to calculate the hash value for the key field in the record - checks if the hash value index in
HashTablecurrently stores an empty record- if the index stores an empty record, store the parameter in this index
- if the index does not store an empty record, store the parameter in
Spare
You can assume there will always be enough space in Spare to store any collisions.
Write program code for InsertIntoHash()
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
The text file HashData.txt stores up to 200 rows of data to be stored into the hash table. Each row contains three integer numbers separated by commas.
The first number is the key field, the second number is item 1 and the third number is item 2.
For example:
The first row in the text file contains: 646, 12, 568
The key field is 646, item 1 is 12 and item 2 is 568
The procedure CreateHashTable():
- opens the file
HashData.txt - creates a record for each row of data in the file
- calls
InsertIntoHash()with each record.
Write program code for CreateHashTable()
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
The procedure PrintSpare() outputs the key field of each element in the array Spare that does not contain an empty record.
Write program code for PrintSpare()
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
The main program should call the procedure to initialise the arrays, call the procedure to create the hash table and then call the procedure to output the contents of the array Spare
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(f)(ii) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(f)(iii) in the evidence document.
Write program code to declare the class Animal and its constructor.
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J25.
Copy and paste the program code into part 3(a)(i) in the evidence document.
The method Description() creates and returns a string of the animal’s data in the format:
"The animal's name is " <Name> ", it makes a " <Sound> ", its size is " <Size> " and its intelligence level is " <Intelligence>
For example:
The animal's name is Teddy, it makes a Bark, its size is 4 and its intelligence level is 6
Write program code for Description()
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
Write program code to declare the class Parrot, its constructor and the method ChangeNumberWords()
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
The method Description() in the class Parrot creates and returns a string of the animal’s data in the format:
"The animal's name is " <Name> ", it makes a " <Sound> ", its size is " <Size> " and its intelligence level is " <Intelligence> ". It has a wingspan of " <WingSpan> "cm and can say " <NumberWords> " words."
For example:
The animal's name is Chewie, it makes a Squawk, its size is 1 and its intelligence level is 10. It has a wingspan of 30cm and can say 29 words.
Write program code for Description()
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
Write program code to declare the class Wolf, its constructor and the method SetTerritorySize()
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
The method Description() in the class Wolf creates and returns a string of the animal’s data in the format:
"The animal's name is " <Name> ", it makes a " <Sound> ", its size is " <Size> " and its intelligence level is " <Intelligence> ". Its territory is " <TerritorySize> " square miles."
For example:
The animal's name is Nighteyes, it makes a Howl, its size is 8 and its intelligence level is 7. Its territory is 100 square miles.
Write program code for Description()
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
The main program declares instances of the classes for three animals:
- A parrot with the name ‘Chewie’; it makes a ‘Squawk’ sound. Its size is 1, intelligence is 10, wingspan is 30cm and it can say 29 words.
- A wolf with the name ‘Nighteyes’; it makes a ‘Howl’ sound. Its size is 8, intelligence is 7 and its territory is 100 square miles.
- An animal that is a horse with the name ‘Copper’; it makes a ‘Neigh’ sound. Its size is 10 and its intelligence is 6.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
The main program also needs to:
- decrease the territory for the wolf Nighteyes by 20 square miles
- increase the number of words the parrot Chewie can say by 2 words
- output the description for all three animals.
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 3(d)(ii) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(d)(iii) in the evidence document.
Write program code to create DataArray and initialise it with the following data values in the order they are written:
0 3 4 56 67 44 43 32 31 345 45 6 54 1
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
The function InsertionSort() takes an array of integers as a parameter. The function sorts the data in the array into ascending numerical order using an insertion sort. The function returns the sorted array.
Write program code for InsertionSort()
You must not use any inbuilt sorting functions for your programming language.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
The procedure OutputArray() takes an array of integers as a parameter. The procedure outputs each element in the array from the first element to the last element. The output is on one line with a space between each number.
An example output is:
"0 3 4 56 67 44 43 32 31 345 45 6 54 1"
Write program code for OutputArray()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
The main program needs extending to:
- output the content of the unsorted array using
OutputArray() - sort the array using
InsertionSort() - output the content of the sorted array using
OutputArray()
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(d)(ii) in the evidence document.
The function Search() performs a binary search to find ItemToFind in DataArray
The function takes two parameters:
DataArray, an array of integersItemToFind, an integer to find inDataArray
The function returns:
- the index of
ItemToFindif it is inDataArray - -1 if
ItemToFindis not inDataArray
Write program code for Search()
You must not use any inbuilt searching functions for your programming language.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
The main program needs extending to call Search() with the sorted array four times:
- the first time to find the index of the integer 0
- the second time to find the index of the integer 345
- the third time to find the index of the integer 67
- the fourth time to find the index of the integer 2
If the integer is found in the array, output an appropriate message that includes the index. If the integer is not found, output that it was not found.
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(f)(ii) in the evidence document.
Write program code to declare Stack, initialise each element in the array with a null value and declare and initialise TopOfStack
Save your program as Question1_N25.
Copy and paste the program code into part 1(a) in the evidence document.
The function Push() takes an integer parameter. If the stack is full, the function returns FALSE. If the stack is not full, the parameter is inserted into the stack, the pointer is updated and the function returns TRUE
Write the program code for Push()
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
The function Pop() returns the next integer in the stack and updates the pointer as appropriate. If there is no data in the stack, the function returns the value –999
Write the program code for Pop()
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
The main program generates 40 random integers between 0 and 1000 (inclusive) and attempts to insert each one into the stack using the appropriate function. If the return value from the function call indicates the stack is full, no more integers are generated and "Stack full" is output.
Write program code for the main program.
Save your program.
Copy and paste the program code into part 1(d) in the evidence document.
The procedure FindValues():
- pops each integer from the stack until the stack is empty
- finds and outputs the largest number that was in the stack in an appropriate message
- finds and outputs the smallest number that was in the stack in an appropriate message.
Write program code for FindValues()
Save your program.
Copy and paste the program code into part 1(e) in the evidence document.
Extend the main program to call FindValues()
Save your program.
Copy and paste the program code into part 1(f)(i) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 1(f)(ii) in the evidence document.
Write program code to declare the class Train and its constructor.
Do not declare the other methods.
All attributes should be private.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_N25.
Copy and paste the program code into part 2(a)(i) in the evidence document.
The methods GetTrainIDNumber() and GetRoute() return the appropriate attribute.
Write program code for GetTrainIDNumber() and GetRoute()
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
The program is tested with four trains:
| train ID number | route |
|---|---|
| 12ADV | 134 |
| 33ART | 20 |
| 9FKF | 3 |
| 21VBC | 24 |
Write program code to declare an instance of Train for each of the four trains.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
Write program code to declare the class Station and its constructor.
Do not declare the other methods.
All attributes should be private.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(c)(i) in the evidence document.
The method AddTrain() takes a Train parameter. The method compares the attributes NumberTrains and NumberPlatforms to identify if there is a platform available (a platform currently with no train).
The method returns FALSE if there are no platforms available.
If there is a platform available, the method:
- stores the
Trainparameter in the arrayTrains - updates the appropriate attribute(s)
- returns
TRUE
Write program code for AddTrain()
Save your program.
Copy and paste the program code into part 2(c)(ii) in the evidence document.
The method GetTrains() returns the string "There are no trains" if there are no trains at the station platforms.
If there are trains at the station platforms, the method returns a string in the format:
The trains at station <StationID> are:
<TrainIDNumber> on route number <Route>
The line <TrainIDNumber> on route number <Route> is repeated for each train at the station platforms.
For example: If the station with the station ID "NT1" has two trains with the train ID numbers "48RTG", "6UFH", the method will produce this output:
The trains at station NT1 are:
48RTG on route number 43
6UFH on route number 12
Write program code for GetTrains()
Save your program.
Copy and paste the program code into part 2(c)(iii) in the evidence document.
Write program code to amend the main program to declare an instance of Station for each of the two stations.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
The four trains attempt to stop at the following stations in the order given:
- Train 12ADV, station STH
- Train 33ART, station STH
- Train 9FKF, station STH
- Train 21VBC, station NTH
Write program code to amend the main program to:
- add each train to the given station using
AddTrain() - output "Station is full" for any train where the return value indicates it cannot be added to the station
- output the trains at each station using
GetTrains()
Save your program.
Copy and paste the program code into part 2(d)(ii) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 2(d)(iii) in the evidence document.
Write program code to declare the class Bird and its constructor.
Do not declare the other methods.
All attributes should be private.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question1_N25.
Copy and paste the program code into part 1(a)(i) in the evidence document.
The method GetSpecies() returns the species of the bird.
Write program code for GetSpecies()
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
The method GetPosition() returns a string of the bird's data in the format:
"X = " & <XPosition> & " Y = " & <YPosition>"
An example string for a bird is:
X = 500.0 Y = 250.0
Write program code for GetPosition()
Save your program.
Copy and paste the program code into part 1(a)(iii) in the evidence document.
If the bird is travelling north, the vertical position increases. If the bird is travelling south, the vertical position decreases.
If the bird is travelling east, the horizontal position increases. If the bird is travelling west, the horizontal position decreases.
The method Move():
- takes the direction of travel as a parameter in the form 'N' for north, 'S' for south, 'E' for east or 'W' for west
- takes the number of minutes that the bird has been flying (to the nearest minute) as a parameter
- calculates the distance travelled by the bird using the formula:
distance travelled = (distance per hour/60) * minutes flying - updates the vertical or horizontal position using the distance calculated.
You do not need to validate the parameters.
Write program code for Move()
Save your program.
Copy and paste the program code into part 1(a)(iv) in the evidence document.
The main program creates two instances of Bird
The first bird is the species 'Cockatiel' and flies 71.0km/h.
The second bird is the species 'Macaw' and flies 56.0km/h.
Write program code to declare and initialise the two birds.
Save your program.
Copy and paste the program code into part 1(b) in the evidence document.
The main program needs to:
- output a message that includes the species and current X position and Y position for each bird
- prompt the user to select one of the birds to move and take this as an input
- prompt the user to enter the direction the bird has been travelling and take this as an input
- prompt the user to enter the time to the nearest minute that the bird has been travelling and take this as an input
- call the appropriate method to update the chosen bird's position
- output an update on the bird's new position.
Each input needs to repeat until valid data is entered. All outputs must be meaningful.
Write program code to amend the main program.
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
Test your program four times to meet these criteria:
Test 1: The Cockatiel travels north for 60 minutes.
Test 2: The Macaw travels south for 30 minutes.
Test 3: The Cockatiel travels west for 30 minutes.
Test 4: The Macaw travels east for 60 minutes.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 1(c)(ii) in the evidence document.
Write program code to declare the array local to the main program and store 20 unique random numbers between 0 and 100 (inclusive) in the array.
Save your program as Question2_N25.
Copy and paste the program code into part 2(a) in the evidence document.
The procedure PrintArray() takes an integer array as a parameter. The procedure outputs the array contents on a single line with a space between each integer.
Write the program code for PrintArray()
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
The function BubbleSort():
- takes an integer array as a parameter
- sorts the data into ascending order using a bubble sort
- returns the sorted array.
The function needs to work for an array of any length.
Do not use an inbuilt sorting method.
Write program code for BubbleSort()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
The main program:
- outputs the contents of the array using
PrintArray() - sorts the array using
BubbleSort() - outputs "Sorted"
- outputs the contents of the sorted array using
PrintArray()
Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 2(d)(ii) in the evidence document.
The recursive function RecursiveBinarySearch() takes four parameters:
- an integer array
- the lower bound of the array
- the upper bound of the array
- the value to find in the array.
The recursive function performs a binary search to find the index of the value in the array.
The function returns the index of the value if it is found. The function returns -1 if the value is not found.
Write program code for RecursiveBinarySearch()
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
The main program:
- prompts the user to enter an integer
- takes the integer as input
- calls
RecursiveBinarySearch()with the sorted array, appropriate lower bound, appropriate upper bound and the user's input as parameters - outputs "Not found" if the input is not within the array
- outputs "Found at position" and the index if the input is within the array.
Write program code to amend the main program.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
Test your program three times with each of the inputs described:
Test 1: the smallest number in the array
Test 2: the largest number in the array
Test 3: a number not in the array
Take a screenshot of each output.
Save your program.
Copy and paste the screenshot(s) into part 2(f)(ii) in the evidence document.
Write program code to declare the class BoardObject and its constructor.
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question1_N25.
Copy and paste the program code into part 1(a)(i) in the evidence document.
The methods GetCode() and GetValue() return the appropriate attribute.
Write program code for GetCode() and GetValue().
Save your program.
Copy and paste the program code into part 1(a)(ii) in the evidence document.
The table shows the code and value of five board objects. The table has the variable identifier where each of the objects are stored.
| Variable identifier | Code | Value |
|---|---|---|
| Object1 | "A" | 2 |
| Object2 | "B" | 3 |
| Object3 | "C" | 5 |
| Object4 | "D" | 2 |
| Object5 | "E" | 7 |
Write program code for the main program to instantiate each of the five board objects and store them in the variables with the identifiers given.
Save your program.
Copy and paste the program code into part 1(a)(iii) in the evidence document.
Write program code to declare the class Board and its constructor.
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 1(b)(i) in the evidence document.
The method GetObject() takes a row number and column number as parameters.
The method returns the BoardObject stored at the parameter position.
Write program code for GetObject()
Save your program.
Copy and paste the program code into part 1(b)(ii) in the evidence document.
The method SetObject() takes three parameters: a BoardObject, row number and column number.
The method stores the BoardObject parameter in the row, column position in TheBoard
Write program code for SetObject()
Save your program.
Copy and paste the program code into part 1(b)(iii) in the evidence document.
The method DisplayBoard() outputs the Code of each BoardObject stored in TheBoard using GetCode()
Each row in TheBoard is output on one line with a space between each Code
For example, the following board contains these four board objects:
- one object has code "A" in row 0 column 7
- one object has code "B" in row 0 column 9
- one object has code "C" in row 1 column 1
- one object has code "E" in row 6 column 5
The other board objects are empty. The output for this board will be:
- - - - - - - A - B
- C - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - E - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Write program code for DisplayBoard()
Save your program.
Copy and paste the program code into part 1(b)(iv) in the evidence document.
The table gives the row and column position on the board to store each of the five objects created in part 1(a)(iii).
| Object identifier | row position | column position |
|---|---|---|
| Object1 | 0 | 0 |
| Object2 | 9 | 9 |
| Object3 | 4 | 5 |
| Object4 | 2 | 2 |
| Object5 | 8 | 7 |
Write program code to amend the main program to:
- declare a new instance of
Board() - store each
BoardObjectin the position given in the table - call
DisplayBoard()for the new board object.
Save your program.
Copy and paste the program code into part 1(c)(i) in the evidence document.
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 1(c)(ii) in the evidence document.
Amend the main program to:
- repeatedly take a row position as input until it is between 0 and 9 inclusive
- repeatedly take a column position as input until it is between 0 and 9 inclusive
- use the appropriate method(s) to identify if there is an object in the array position input
- output "Miss" if there is an empty
BoardObjectin that position - output the
CodeandValueif there is a non-emptyBoardObjectin that position.
All outputs must include appropriate messages.
Save your program.
Copy and paste the program code into part 1(d)(i) in the evidence document.
Test your program by entering this test data in the order given:
Row position first input: 10
Row position second input: 4
Column position first input: -1
Column position second input: 5
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot(s) into part 1(d)(ii) in the evidence document.